﻿Imports Microsoft.VisualBasic
Imports nce.adosql
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Configuration
Imports System.Net.Mail


Public Class Functions
    Inherits System.Web.UI.Page
    Public countryID As Object
    Public RecSet As ADODB.Recordset
    Public RS As ADODB.Recordset
    Public Conn As New ADODB.Connection

    Public Property UsernamePartnerSettings As String
    Public Property PasswordPartnerSettings As String


    Public myMail As MailMessage
    Public mailfrom As MailAddress
    Public mailto As MailAddress

    Public fragusLeasingUserID As Object ' System.Object
    Public fragusLeasingUserTypeID As Object ' System.Object
    Public strJS As Object
    Public strSelectSubHeadline As Object
    Public strSelectSubJS As Object
    Public mobileGUI As Object ' System.Object
    Public coCompany As Object
    Public coStreet As Object
    Public coZipcode As Object
    Public coCity As Object
    Public coTelephone As Object
    Public coFax As Object
    Public coEmail As Object
    Public coHomepage As Object
    Public coCountry As Object
    Public coCin As Object
    Public intClientID As Object


    Protected Overridable Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Response.Buffer = True
        Session.LCID = 1053
        fragusLeasingUserID = Session("fragusLeasingUserID")
        fragusLeasingUserTypeID = Session("fragusLeasingUserTypeID")
        mobileGUI = Session("mobileGUI")
        If Len(fragusLeasingUserID) = 0 Or Len(fragusLeasingUserTypeID) = 0 Then
            Response.Redirect("logOut.aspx")
        End If
        Session.Add("fragusLeasingUserID", fragusLeasingUserID)
        Session.Add("fragusLeasingUserTypeID", fragusLeasingUserTypeID)
        Session.Add("mobileGUI", mobileGUI)
        Session.Timeout = 120

    End Sub
    Enum enumObjectType
        StrType = 0
        IntType = 1
        DblType = 2
    End Enum

    Public Function CheckDBNull(ByVal obj As Object, _
    Optional ByVal ObjectType As enumObjectType = enumObjectType.StrType) As Object
        Dim objReturn As Object
        objReturn = obj
        If ObjectType = enumObjectType.StrType And IsDBNull(obj) Then
            objReturn = ""
        ElseIf ObjectType = enumObjectType.IntType And IsDBNull(obj) Then
            objReturn = 0
        ElseIf ObjectType = enumObjectType.DblType And IsDBNull(obj) Then
            objReturn = 0.0
        End If
        Return objReturn
    End Function

    Public Function IsNewPricelistContract(vehicleID As Integer) As Boolean

        Dim isNewPrice As Boolean = False
        dbOpen()

        Dim sqlstr As String = "SELECT        b.brandid,m.modelid,v.clientID, v.regnr, v.vinNumber, v.cylinderVolume,v.isDieselcar, v.date, b.brand, m.model, u.username, tblPriceList.ServiceIntervall, tblPriceList.isDieselcar" +
                 "   FROM            tblVehicle AS v INNER JOIN " +
                 "            tblBrand AS b ON b.brandID = v.brandID INNER JOIN " +
                 "            tblModel AS m ON m.modelID = v.modelID INNER JOIN " +
                 "            tblUser AS u ON u.userID = v.userID INNER JOIN " +
                 "            tblPriceList ON v.priceListID = tblPriceList.priceListID " +
        String.Format(" Where v.vehicleID ={0}", vehicleID)

        '  RecSet = Conn.Execute("Select v.clientID, v.regnr, v.vinNumber, v.cylinderVolume, v.date, b.brand, m.model, u.username From (((tblVehicle v INNER JOIN tblBrand b ON b.brandID = v.brandID) INNER JOIN tblModel m ON m.modelID = v.modelID) INNER JOIN tblUser u ON u.userID = v.userID) Where v.vehicleID = " & vehicleID & "")
        RecSet = Conn.Execute(sqlstr)

        If RecSet.EOF Then
            Return False
        Else

            Dim brandID As Object = RecSet.Fields("brandid").Value
            Dim modelID As Object = RecSet.Fields("modelid").Value

            If (brandID = 118 And modelID = 1462) Then
                isNewPrice = True
            End If
        End If
        Return isNewPrice
    End Function

    Public Function GetSalesTax(RecSet As ADODB.Recordset, __date As Date) As Object
        Dim salesTax As Object
        salesTax = -1
        If (IsDBNull(RecSet.Fields("salesTax"))) Then
            If (__date <= Date.Parse("2010-06-30")) Then
                salesTax = 22
            ElseIf (__date > Date.Parse("2010-06-30") And __date < Date.Parse("2013-01-08")) Then
                salesTax = 23
            End If
        Else
            salesTax = RecSet.Fields("salesTax").Value
        End If
        Return salesTax
    End Function

    Sub dbOpen()
        Conn = New ADODB.Connection()

        Conn.Open(ConfigurationManager.ConnectionStrings("DatabaseConnection").ToString())
    End Sub

    Sub dbClose()
        Conn.Close()
        Conn = Nothing
    End Sub
    Sub SendmailMailMessage(ByVal message As System.Net.Mail.MailMessage)

        Dim clientsend As New SmtpClient
        clientsend.Port = ConfigurationManager.AppSettings("mailserverport")
        clientsend.Host = ConfigurationManager.AppSettings("mailserver")
        clientsend.Send(message)
    End Sub
    Protected Overridable Sub SetUsernamePassword()
        UsernamePartnerSettings = "sdfnisse"
        PasswordPartnerSettings = "adekalle"

    End Sub
    Protected Overridable Sub Over()
        SetUsernamePassword()
        Response.Write("<html>")
        Response.Write("<head>")
        Response.Write("<title>Fragus Leasing Systems - administration</title>")
        'Response.Write "<script language='JavaScript' src='fieldFocus.js'></script>"
        Response.Write("<link rel='stylesheet' href='style.css'>")
        Response.Write("<script type=""text/javascript"">" & vbCr)
        Response.Write("	  function OnLoadFunctions(){" & vbCr)
        Response.Write("	    //hide loading status..." & vbCr)
        Response.Write("	    document.getElementById(""loading"").style.display='none';" & vbCr)
        Response.Write("	    //show content" & vbCr)
        Response.Write("	    document.getElementById(""content"").style.display='block';" & vbCr)
        Response.Write("	    if (document.forms.length > 0) {" & vbCr)
        Response.Write("	      var field = document.forms[0];" & vbCr)
        Response.Write("	      for (i = 0; i < field.length; i++) {" & vbCr)
        Response.Write("		if ((field.elements[i].type == ""text"") || (field.elements[i].type == ""textarea"") || (field.elements[i].type.toString().charAt(0) == ""s"")) {" & vbCr)
        Response.Write("		  document.forms[0].elements[i].focus();" & vbCr)
        Response.Write("		  break;" & vbCr)
        Response.Write("		}" & vbCr)
        Response.Write("	      }" & vbCr)
        Response.Write("	    }" & vbCr)
        Response.Write("	  }" & vbCr)
        Response.Write("	</script>" & vbCr)
        Response.Write("</head>")
        Response.Write("<body OnLoad=""OnLoadFunctions()"">")
        Response.Write("<script type=""text/javascript"">" & vbCr)
        Response.Write("	  document.write('<div id=""loading""><center><img src=""gui/progressBar.gif""><br><br>System loading. Please wait...</center></div>');" & vbCr)
        Response.Write("	</script>" & vbCr)
        Response.Write("	<div id=""content"">" & vbCr)
        Response.Write("<script type=""text/javascript"">" & vbCr)
        Response.Write("	    //hide content" & vbCr)
        Response.Write("	    document.getElementById(""content"").style.display='none';" & vbCr)
        Response.Write("	  </script>" & vbCr)
        If CDbl(mobileGUI) = 0 Then
            Response.Write("<script language='JavaScript' src='menu.js'></script>")
            Response.Write("<script language='JavaScript' src='menu_items.js'></script>")
            Response.Write("<script language='JavaScript' src='menu_tpl.js'></script>")
            Response.Write("<script language='JavaScript'>")
            Response.Write("new menu (MENU_ITEMS, MENU_POS);")
            Response.Write("</script>")
            Response.Write("<table height='100' width='100%' cellpadding='0' cellspacing='0' background='gui/menu_main.jpg'>")
            Response.Write("<tr>")
            Response.Write("<td align='left'><img src='gui/menu_left.jpg'></td>")
            Response.Write("<td align='right'><img src='gui/menu_right.jpg'></td>")
            Response.Write("</tr>")
            Response.Write("</table>")
        Else
            Response.Write("<table height='20' width='100%' cellpadding='0' cellspacing='0'>")
            Response.Write("<tr>")
            Response.Write("<td class='headline'><a href='userInside.aspx'>[GO TO MENU]</a><hr class='hrHeadline'></td>")
            Response.Write("</tr>")
            Response.Write("</table>")
        End If
    End Sub

    Sub OverPrint()
        Response.Write("<html>")
        Response.Write("<head>")
        Response.Write("<title>FLS - Print</title>")
        Response.Write("<link href='style.css' rel='stylesheet' type='text/css'>")
        Response.Write("  <style media=""print"">" & vbCr)
        Response.Write("  .noprint { display: none }" & vbCr)
        Response.Write("  </style>" & vbCr)
        Response.Write("<script defer>" & vbCr)
        Response.Write("  function window.onload() {" & vbCr)
        Response.Write("    factory.printing.header = """"" & vbCr)
        Response.Write("    factory.printing.footer = """"" & vbCr)
        Response.Write("    factory.printing.portrait = true" & vbCr)
        Response.Write("  }" & vbCr)
        Response.Write("    function printWindow() {" & vbCr)
        Response.Write("    bV = parseInt(navigator.appVersion);" & vbCr)
        Response.Write("    if (bV >= 4) window.print();return true" & vbCr)
        Response.Write("  }" & vbCr)
        Response.Write("  </script>" & vbCr)
        Response.Write("<object id=""factory"" viewastext  style=""display:none""" & vbCr)
        Response.Write("    classid=""clsid:1663ed61-23eb-11d2-b92f-008048fdd814""" & vbCr)
        Response.Write("    codebase=""smsx.cab#Version=6,3,435,20"">" & vbCr)
        Response.Write("  </object>" & vbCr)
        Response.Write("</head>")
        Response.Write("<body>")
    End Sub


    Protected Overridable Sub Under()
        Response.Write("</div>")
        Response.Write("</body>")
        Response.Write("</html>")
    End Sub

    Sub CompanyAddressSQL()
        dbOpen()
        RecSet = Conn.Execute("Select co.company, co.street, co.zipcode, co.city, co.telephone, co.fax, co.email, co.homepage, co.cin, c.country From (tblCompany co INNER Join tblCountry c ON c.countryID = co.companyID) Where co.countryID = " & countryID & "")
        If Not RecSet.EOF Then
            coCompany = RecSet.Fields("company").Value
            coStreet = RecSet.Fields("street").Value
            coZipcode = RecSet.Fields("zipcode").Value
            coCity = RecSet.Fields("city").Value
            coTelephone = RecSet.Fields("telephone").Value
            coFax = RecSet.Fields("fax").Value
            coEmail = RecSet.Fields("email").Value
            coHomepage = RecSet.Fields("homepage").Value
            coCountry = RecSet.Fields("country").Value
            coCin = RecSet.Fields("cin").Value
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
    End Sub

    Sub CompanyAddressPrintAllSQL()
        RecSet = Conn.Execute("Select co.company, co.street, co.zipcode, co.city, co.telephone, co.fax, co.email, co.homepage, co.cin, c.country From (tblCompany co INNER Join tblCountry c ON c.countryID = co.companyID) Where co.countryID = " & countryID & "")
        If Not RecSet.EOF Then
            coCompany = RecSet.Fields("company").Value
            coStreet = RecSet.Fields("street").Value
            coZipcode = RecSet.Fields("zipcode").Value
            coCity = RecSet.Fields("city").Value
            coTelephone = RecSet.Fields("telephone").Value
            coFax = RecSet.Fields("fax").Value
            coEmail = RecSet.Fields("email").Value
            coHomepage = RecSet.Fields("homepage").Value
            coCountry = RecSet.Fields("country").Value
            coCin = RecSet.Fields("cin").Value
        End If
        RecSet.Close()
        RecSet = Nothing
    End Sub

    Sub CompanyAddress()
        Response.Write("<table cellpadding='0' cellspacing='0'>")
        Response.Write("<tr><td colspan='2'><img src='gui/fragus_logo.jpg'></td></tr>")
        Response.Write("<tr><td><br></td></tr>")
        Response.Write("<tr><td colspan='2' class='printBold'>" & coCompany & "</td></tr>")
        Response.Write("<tr><td><br></td></tr>")
        Response.Write("<tr><td class='printRegular' valign='top'>Address:</td><td class='printRegular'>" & coStreet & "<br>" & coZipcode & " " & coCity & "<br>" & coCountry & "</td></tr>")
        Response.Write("<tr><td class='printRegular'>Phone:</td><td class='printRegular'>" & coTelephone & "</td></tr>")
        Response.Write("<tr><td class='printRegular'>Fax:</td><td class='printRegular'>" & coFax & "</td></tr>")
        Response.Write("<tr><td class='printRegular'>E-mail:</td><td class='printRegular'>" & coEmail & "</td></tr>")
        Response.Write("<tr><td class='printRegular'>Homepage:</td><td class='printRegular'>" & coHomepage & "</td></tr>")
        Response.Write("<tr><td class='printRegular'>Company ID:</td><td class='printRegular'>" & coCin & "</td></tr>")
        Response.Write("<tr><td><br></td></tr>")
        Response.Write("<tr><td colspan='2' class='printRegular'>" & coCompany & ", a subsidiary to AB Svensk Bilgaranti</td></tr>")
        Response.Write("</table>")
    End Sub

    Sub checkFields()
        Response.Write("<script type=""text/javascript"">" & vbCr)
        Response.Write("<!--" & vbCr)
        Response.Write("function _(id) {" & vbCr)
        Response.Write("    return document.getElementById(id);" & vbCr)
        Response.Write("}" & vbCr)
        Response.Write("function scrollToError(elemId)" & vbCr)
        Response.Write("{" & vbCr)
        Response.Write("    if (window.scrollTo) {" & vbCr)
        Response.Write("	var el = _(elemId);" & vbCr)
        Response.Write("	var pos = 0;" & vbCr)
        Response.Write("	// Calculate the position" & vbCr)
        Response.Write("	while (el) {" & vbCr)
        Response.Write("            pos += el.offsetTop;" & vbCr)
        Response.Write("            el = el.offsetParent;" & vbCr)
        Response.Write("	}" & vbCr)
        Response.Write("	if (typeof(document.body.topMargin) == 'number')" & vbCr)
        Response.Write("            pos += document.body.topMargin;" & vbCr)
        Response.Write("        if (pos > 100)" & vbCr)
        Response.Write("            pos -= 100;" & vbCr)
        Response.Write("        " & vbCr)
        Response.Write("	// Scroll" & vbCr)
        Response.Write("        window.scrollTo(0, pos);" & vbCr)
        Response.Write("    }" & vbCr)
        Response.Write("    // Focus " & vbCr)
        Response.Write("    _(elemId).focus();" & vbCr)
        Response.Write("} " & vbCr)
        Response.Write("// sorted in top-to-bottom order" & vbCr)
        Response.Write("var checks = [")
        Response.Write(strJS)
        Response.Write("];" & vbCr)
        Response.Write("var error;" & vbCr)
        Response.Write("var firstfield;" & vbCr)
        Response.Write("function badfield( _field )" & vbCr)
        Response.Write("{" & vbCr)
        Response.Write("    _(""error_""+_field).style.display = 'block';" & vbCr)
        Response.Write("    if (!error) {" & vbCr)
        Response.Write("        firstfield = _field;" & vbCr)
        Response.Write("        error=true;" & vbCr)
        Response.Write("    }" & vbCr)
        Response.Write("}" & vbCr)
        Response.Write("function checkFields()" & vbCr)
        Response.Write("{" & vbCr)
        Response.Write("    error = false;" & vbCr)
        Response.Write("    firstfield = null;" & vbCr)
        Response.Write("    try {" & vbCr)
        Response.Write("        for (i=0; i<checks.length; i++) {" & vbCr)
        Response.Write("            var field = checks[i][0];" & vbCr)
        Response.Write("            var condition = checks[i][1];" & vbCr)
        Response.Write("            var value = _(field).value;" & vbCr)
        Response.Write("            " & vbCr)
        Response.Write("            // check that field is non-empty" & vbCr)
        Response.Write("            if (value && value.length < 1) {" & vbCr)
        Response.Write("                badfield(field);" & vbCr)
        Response.Write("                continue;" & vbCr)
        Response.Write("            }" & vbCr)
        Response.Write("            // check that field is correct" & vbCr)
        Response.Write("            expr = ""'"" + value + ""'"" + condition;" & vbCr)
        Response.Write("            if (eval(expr) == true) {" & vbCr)
        Response.Write("                badfield(field);" & vbCr)
        Response.Write("                continue;" & vbCr)
        Response.Write("            }" & vbCr)
        Response.Write("            _(""error_""+field).style.display = 'none';" & vbCr)
        Response.Write("        }" & vbCr)
        Response.Write("        if (error)" & vbCr)
        Response.Write("            scrollToError(firstfield);" & vbCr)
        Response.Write("    }" & vbCr)
        Response.Write("    catch (e) {" & vbCr)
        Response.Write("        //alert(e.message ? e.message : e.description);" & vbCr)
        Response.Write("    }" & vbCr)
        Response.Write("        " & vbCr)
        Response.Write("    return !error;" & vbCr)
        Response.Write("}" & vbCr)
        Response.Write("-->" & vbCr)
        Response.Write("</script>" & vbCr)
    End Sub

    Sub insertFonts()
        Response.Write("<script type=""text/javascript"">" & vbCr)
        Response.Write("<!--")
        Response.Write("var carret=null,fieldobj=null,ie=(document.selection&&document.selection.createRange);")
        Response.Write("function storepos(obj){ ")
        Response.Write("   fieldobj=obj; carret=(ie)?document.selection.createRange():null; ")
        Response.Write("}")
        Response.Write("function saveScrollCoordinates()")
        Response.Write("{")
        Response.Write("	//scrollx = (document.all)?document.body.scrollLeft:window.pageXOffset;")
        Response.Write("	//scrolly = (document.all)?document.body.scrollTop:window.pageYOffset;")
        Response.Write("}")
        Response.Write("function insertAtCarret(str){")
        Response.Write("   if(!fieldobj) return null;")
        Response.Write("   if (carret){")
        Response.Write("      carret.text=str; carret.select();")
        Response.Write("   } else if(document.getElementById&&!ie) {")
        Response.Write("      fieldobj.value=fieldobj.value.substring(0,fieldobj.selectionStart)+str+fieldobj.value.substring(fieldobj.selectionEnd);")
        Response.Write("      fieldobj.focus();")
        Response.Write("   } else {")
        Response.Write("      fieldobj.value+=str;")
        Response.Write("      fieldobj.focus();")
        Response.Write("   }")
        Response.Write("}")
        Response.Write("-->")
        Response.Write("</script>" & vbCr)
    End Sub

    Sub selectSub()
        Response.Write("<script language=""JavaScript"">" & vbCr)
        Response.Write("var DomYes=document.getElementById?1:0;" & vbCr)
        Response.Write("function set_child_listbox(parentObject,childObject,childArray,spanToHide) {" & vbCr)
        Response.Write("	//Clear child listbox" & vbCr)
        Response.Write("	for(var i=childObject.length;i>0;i--) {" & vbCr)
        Response.Write("		childObject.options[i] = null;" & vbCr)
        Response.Write("	} " & vbCr)
        Response.Write("	" & vbCr)
        Response.Write("	childObject.options[0] = new Option("" ")
        Response.Write(strSelectSubHeadline)
        Response.Write(" "","""");" & vbCr)
        Response.Write("	var sel_index = parentObject.options[parentObject.selectedIndex].value;" & vbCr)
        Response.Write("	if (sel_index == """") {" & vbCr)
        Response.Write("		childObject.disabled = true;" & vbCr)
        Response.Write("	} else {" & vbCr)
        Response.Write("		childObject.disabled = false;" & vbCr)
        Response.Write("		var childIndex = 1;" & vbCr)
        Response.Write("		for (i = 0; i < childArray.length; i++) {" & vbCr)
        Response.Write("			if (childArray[i][1] == sel_index) {" & vbCr)
        Response.Write("				childObject.options[childIndex] = new Option(childArray[i][2], childArray[i][0]);" & vbCr)
        Response.Write("				childIndex++;" & vbCr)
        Response.Write("			}" & vbCr)
        Response.Write("		}" & vbCr)
        Response.Write("	}" & vbCr)
        Response.Write("	//Select first option" & vbCr)
        Response.Write("	childObject.selectedIndex = 0;" & vbCr)
        Response.Write("}" & vbCr)
        Response.Write("var Product = new Array(")
        Response.Write(strSelectSubJS)
        Response.Write(");" & vbCr)
        Response.Write("</script>" & vbCr)
    End Sub




End Class
